/* Copyright 2010 John L. Reilly

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

package com.riq.nonpersist;

import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.riq.entity.Location;
import com.riq.entity.Member;
import com.riq.entity.Status;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ReportLocation implements Comparable<ReportLocation> {
    
    private static final long serialVersionUID = 1L;
    
          @PrimaryKey
          @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
          private Key key;
          @NotPersistent
          private final String activeFlag;
          @NotPersistent
          private final Location location;
          @NotPersistent
          private List<Status> locationStatuses;
          @NotPersistent
          private Long selectedStatus;
          @NotPersistent
          private List<Member> locationMembers;

        public ReportLocation(
                String activeFlag, 
                Location location,
                List<Status> locationStatuses,
                Long selectedStatus,
                List<Member> deptLocMembers
                ) {
            
            if (location == null )
                throw new NullPointerException();
            
            this.activeFlag = activeFlag;
            this.location = location;
            this.locationStatuses = locationStatuses;
            this.selectedStatus = selectedStatus;
            this.locationMembers = deptLocMembers;
        }

        public String alertId()  { return activeFlag;  }
        public Location location()  { return location;  }
        public List<Status> locationStatuses() { return locationStatuses; }
        public Long selectedStatus()  { return selectedStatus;  }
        public List<Member> locMembers()  { return locationMembers;  }

        public boolean equals(Object o) {
            if (!(o instanceof ReportLocation))
                return false;
            ReportLocation t = (ReportLocation)o;
            return t.activeFlag.equals(activeFlag) &&
                   t.location == location &&
                   t.locationStatuses == locationStatuses &&
                   t.selectedStatus == selectedStatus &&
                   t.locationMembers == locationMembers;
        }
        
        
        
        private int sortingKey() {
            return location.getsequence();
        }

//        public int compareTo(ReportLocation t) {
//            return sortingKey().compareTo(t.sortingKey());
//        }
        
        public int compareTo( ReportLocation aThat ) {
          final int BEFORE = -1;
          final int EQUAL = 0;
          final int AFTER = 1;

          //this optimization is usually worthwhile, and can
          //always be added
          if ( this == aThat ) return EQUAL;

          //primitive numbers follow this form
          if (this.location.getsequence() < aThat.location.getsequence()) return BEFORE;
          if (this.location.getsequence() > aThat.location.getsequence()) return AFTER;

//          //all comparisons have yielded equality
//          //verify that compareTo is consistent with equals (optional)
//          assert this.equals(aThat) : "compareTo inconsistent with equals.";

          return EQUAL;
        }

}